{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4c7a4dd0-07d2-4275-a9e9-26453ea8f847",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------after elimination------\n",
      "[[1.0, 2.0, 1.0], [0.0, -2.0, 1.0], [0.0, 0.0, 5.0]]\n",
      "[0.0, 3.0, 3.0]\n",
      "x= [1.7999999999999998, -1.2, 0.6]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "def gaussian_elimination(A,b):\n",
    "        n=len(A)\n",
    "        for k in range(n):\n",
    "                for i in range(k+1,n):\n",
    "                        if np.abs(A[k][k])<1E-6:\n",
    "                                print('pivot=0')\n",
    "                                exit\n",
    "                        factor=A[i][k]/A[k][k]\n",
    "                        for j in range(n):\n",
    "                                A[i][j]=A[i][j]-factor*A[k][j]\n",
    "                        b[i]=b[i]-factor*b[k]\n",
    "        print('------after elimination------')\n",
    "        print(A)\n",
    "        print(b)\n",
    "\n",
    "        x=[0.0 for i in range(n)]\n",
    "        for i in range(n-1,-1,-1):\n",
    "                x[i]=b[i]\n",
    "                for j in range(i+1,n):\n",
    "                        x[i]=x[i]-A[i][j]*x[j]\n",
    "                x[i]=x[i]/A[i][i]\n",
    "        print('x=',x)\n",
    "\n",
    "if __name__=='__main__':\n",
    "        A0=[[1.0,2.0,1.0],[2.0,2.0,3.0],[-1.0,0.0,3.0]]\n",
    "        b0=[0.0,3.0,0.0]\n",
    "        gaussian_elimination(A0,b0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "678b2e92-7a8d-4a57-81ef-3a28b81df5cd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0.0, 0.0, 0.0]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n=3\n",
    "x=[0.0 for i in range(n)]\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "db1f6e78-a0a5-418a-b97a-675ea51c47a8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
